#include "MarsPrecompile.h"

#include "MarsFbxMeshImporter.h"
#include "MarsFbxMgr.h"

using namespace Pipeline;
using namespace Graphics;

Mesh* FbxMeshImporter::Import( const std::string& strMeshFileName )
{
	int lFileMajor, lFileMinor, lFileRevision;
	int lSDKMajor,  lSDKMinor,  lSDKRevision;
	//int lFileFormat = -1;

	// Get the file version number generate by the FBX SDK.
	FbxManager::GetFileFormatVersion( lSDKMajor, lSDKMinor, lSDKRevision );

	FbxManager* pFbxManager = FbxMgr::Instance().GetFbxManager();
	MARS_ASSERT( pFbxManager );

	FbxScene* pFbxScene = FbxMgr::Instance().GetFbxScene();
	MARS_ASSERT( pFbxScene );

	// Create an importer.
	FbxImporter* lImporter = FbxImporter::Create( pFbxManager,"" );

	// Initialize the importer by providing a filename.
	const bool lImportStatus = lImporter->Initialize( strMeshFileName.c_str(), -1, pFbxManager->GetIOSettings() );
	lImporter->GetFileVersion( lFileMajor, lFileMinor, lFileRevision );

	if ( !lImportStatus )
	{
		FbxString error = lImporter->GetStatus().GetErrorString();
		FBXSDK_printf( "Call to FbxImporter::Initialize() failed.\n" );
		FBXSDK_printf( "Error returned: %s\n\n", error.Buffer() );

		if ( lImporter->GetStatus().GetCode() == FbxStatus::eInvalidFileVersion )
		{
			FBXSDK_printf( "FBX file format version for this FBX SDK is %d.%d.%d\n", lSDKMajor, lSDKMinor, lSDKRevision );
			FBXSDK_printf( "FBX file format version for file '%s' is %d.%d.%d\n\n", strMeshFileName.c_str(), lFileMajor, lFileMinor, lFileRevision );
		}

		return NULL;
	}

	FBXSDK_printf( "FBX file format version for this FBX SDK is %d.%d.%d\n", lSDKMajor, lSDKMinor, lSDKRevision );

	if ( lImporter->IsFBX() )
	{
		FBXSDK_printf( "FBX file format version for file '%s' is %d.%d.%d\n\n", strMeshFileName.c_str(), lFileMajor, lFileMinor, lFileRevision );

		// From this point, it is possible to access animation stack information without
		// the expense of loading the entire file.

		FBXSDK_printf( "Animation Stack Information\n" );

		int lAnimStackCount = lImporter->GetAnimStackCount();

		FBXSDK_printf( "    Number of Animation Stacks: %d\n", lAnimStackCount );
		FBXSDK_printf( "    Current Animation Stack: \"%s\"\n", lImporter->GetActiveAnimStackName().Buffer() );
		FBXSDK_printf( "\n" );

		for ( int i = 0; i < lAnimStackCount; i++ )
		{
			FbxTakeInfo* lTakeInfo = lImporter->GetTakeInfo( i );

			FBXSDK_printf( "    Animation Stack %d\n", i );
			FBXSDK_printf( "         Name: \"%s\"\n", lTakeInfo->mName.Buffer() );
			FBXSDK_printf( "         Description: \"%s\"\n", lTakeInfo->mDescription.Buffer() );

			// Change the value of the import name if the animation stack should be imported 
			// under a different name.
			FBXSDK_printf( "         Import Name: \"%s\"\n", lTakeInfo->mImportName.Buffer() );

			// Set the value of the import state to false if the animation stack should be not
			// be imported. 
			FBXSDK_printf( "         Import State: %s\n", lTakeInfo->mSelect ? "true" : "false" );
			FBXSDK_printf( "\n" );
		}

		// Set the import states. By default, the import states are always set to 
		// true. The code below shows how to change these states.
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_MATERIAL,        true );
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_TEXTURE,         true );
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_LINK,            true );
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_SHAPE,           true );
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_GOBO,            true );
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_ANIMATION,       true );
		pFbxManager->GetIOSettings()->SetBoolProp( IMP_FBX_GLOBAL_SETTINGS, true );
	}

	// Import the scene.
	bool lStatus = lImporter->Import( pFbxScene );

	// Destroy the importer.
	lImporter->Destroy();

	return NULL;
}
